package com.app.player;

import android.app.ProgressDialog;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Handler;
import android.widget.ProgressBar;

public class PlayerMp3 implements OnCompletionListener{

	private MediaPlayer player;

	private Context contexto;
	private static final int NOVO=0;
	private static final int TOCANDO=1;
	private static final int PAUSADO=2;
	private static final int PARADO=4;
	private int status = NOVO;
	private String mp3;
	private boolean terminou = false;
	
	public PlayerMp3(){
		player = new MediaPlayer();
		player.setOnCompletionListener(this);
	}
	
	public void iniciar(String mp3){
		this.mp3 = mp3;
		try{
			player.setDataSource(mp3);
			player.prepare();
			player.start();
		}catch(Exception e){
			e.printStackTrace();
		}
		terminou = false;
	}
	
	public void tocar(){
		player.start();
	}
	public void pausar(){
		player.pause();
	}
	public void parar(){
		player.stop();
		player.reset();
		
	}
	
	public void fechar(){
		player.stop();
		player.release();
		player = null;
		System.out.println("Player destruido");
	}

	public String getMusicaTocando(){
		return mp3;
	}
	
	public int getDuracao(){
		return player.getDuration();
	}
	
	public void passaMusica(){
		player.stop();
		player.reset();
		this.status = NOVO;
	}
	
	public int getPosicaoMusica(){
		return player.getCurrentPosition();
	}
	
	@Override
	public void onCompletion(MediaPlayer arg0) {
		// TODO Auto-generated method stub
		terminou = true;
		System.out.println("Musica terminou");
	}
		
	public boolean isTocando(){
		return player.isPlaying();
	}
	
	public void setPosicaoMusica(int posicao){
		player.seekTo(posicao);
	}

	public boolean isTerminou() {
		return terminou;
	}

	public void setTerminou(boolean terminou) {
		this.terminou = terminou;
	}
	
	
	
}
